package com.xxrl.hmwork.controller;

import com.xxrl.hmwork.commons.StringUtils;
import com.xxrl.hmwork.domain.User;
import com.xxrl.hmwork.service.FileService;
import com.xxrl.hmwork.vo.FileItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 负责云文件的list和删除
 * @author mis
 */
@RestController
@RequestMapping("/cloud")
public class CloudController {

    private final FileService fileService;

    @Autowired
    public CloudController(FileService fileService) {
        this.fileService = fileService;
    }

    @RequestMapping("list")
    public List<FileItem> list(String user, HttpSession session) {
        if (StringUtils.isEmpty(user)) {
            User obj = (User)session.getAttribute("user");
            if (obj == null) {
                return null;
            }
            user = obj.getName();
            if (user == null) {
                return null;
            }
        }
        return fileService.listCloudFile(user);
    }

    @RequestMapping("delete")
    public void del(String user,String file) {
        fileService.deleteCloudFile("/"+user+"/"+file);
    }

    @GetMapping("download")
    public void download(HttpServletResponse response, String user, String file) {
        fileService.downloadCloud(response,user+'/'+file);
    }
}

